﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShopOnline.Models.DAL;
using ShopOnline.Models;
using ShopOnline.Models.Utils;

namespace ShopOnline.Controllers
{
    public class AdvertController : Controller
    {
        private static int Advertcurent = 0;

        public int advertcurent
        {
            get { return Advertcurent; }
            set { Advertcurent = value; }
        }
        private DAL_Advert AdvertRepository;
        private ShopOnline.Models.ShopOnlineDBDataContext db;
        public AdvertController()
        {
            AdvertRepository = new DAL_Advert();
            db = new Models.ShopOnlineDBDataContext();
        }
        //
        // GET: /Picture/
        [Authorize]
        public ActionResult AdminListAdvert()
        {
            var list = AdvertRepository.getListAdvert();
            return View(list);
        }

        //
        // GET: /Picture/Details/5
        [Authorize]
        public ActionResult Details(int id)
        {
            var picture = AdvertRepository.getAdvertById(id);
            return View(picture);
        }

        //
        // GET: /Picture/Create
        [Authorize]
        public ActionResult Create()
        {

            return View();
        } 

        //
        // POST: /Picture/Create
               [Authorize]
               [HttpPost]
               public ActionResult Create(FormCollection collection)
               {
                   try
                   {
                       Advert par = new Advert();
                       par.Description = collection["description"];
                       par.AdvertName = collection["AdvertName"];
                       String pri = collection["Priority"];
                       try
                       {
                           par.Priority = int.Parse(pri.Trim());
                       }
                       catch (Exception)
                       {
                           par.Priority = 1;
                       }

                       par.Link = collection["url"];
                       var show = collection["show"];
                       if (show=="false")
                       {
                           par.Show = false;
                       }
                       else
                       {
                           par.Show = true;
                       }

                       #region upload images
                            Uploader uploader = new Uploader();
                            uploader.UploadPath = Server.MapPath("~\\Content\\AdvertImages");
                           uploader.IsLowerName = false;
                           uploader.IsEncryptName = false;
                           uploader.IsOverwrite = false;
                           uploader.Prefix = "";
                           uploader.Suffix = "";

                           uploader.AllowedExtensions.Add(".jpg");
                           uploader.AllowedExtensions.Add(".jpeg");
                           uploader.AllowedExtensions.Add(".gif");
                           uploader.AllowedExtensions.Add(".png");
           
                           bool success = uploader.DoUpload("MyFile");
                       #endregion
                           par.Image = uploader.PostedFile.FileName;

                           db.Adverts_add(par.AdvertName, par.Description, par.Image, par.Link, par.Priority, par.Show);
                           return RedirectToAction("AdminListAdvert");
                   }
                   catch
                   {
                       return View();
                   }
               }
        
               //
               // GET: /Picture/Edit/5
                        [Authorize]
                        public ActionResult Edit(int id)
                        {
                            var pic = AdvertRepository.getAdvertById(id);
                            return View(pic);
                        }

                        //
                        // POST: /Picture/Edit/5

                        [HttpPost]
                        public ActionResult Edit(int id, FormCollection collection)
                        {
                            try
                            {
                                // TODO: Add update logic here
                                Advert par = AdvertRepository.getAdvertById(id);
                                par.Description = collection["description"];
                                par.AdvertName = collection["AdvertName"];
                                String pri = collection["Priority"];
                                try
                                {
                                    par.Priority = int.Parse(pri.Trim());
                                }
                                catch (Exception)
                                {
                                    par.Priority = 1;
                                }

                                par.Link = collection["url"];
                                var show = collection["show"];
                                if (show == "false")
                                {
                                    par.Show = false;
                                }
                                else
                                {
                                    par.Show = true;
                                }

                                
                                db.Adverts_update(par.Id, par.AdvertName, par.Description, par.Image, par.Link, par.Priority, par.Show);
                                return RedirectToAction("AdminListAdvert");
                            }
                            catch
                            {
                                var pic = AdvertRepository.getAdvertById(id);
                                return View(pic);
                            }
                        }

                        //
                        // GET: /Picture/Delete/5
     [Authorize]
                             public ActionResult Delete(int id)
                             {
                                 var pic = AdvertRepository.getAdvertById(id);
                                 return View(pic);
                             }

                             //
                             // POST: /Picture/Delete/5

                             [HttpPost]
                             public ActionResult Delete(int id, FormCollection collection)
                             {
                                 try
                                 {
                                     // TODO: Add delete logic here
                                     var pic = AdvertRepository.getAdvertById(id);
                                     db.Adverts_delete(pic.Id);
                                     return RedirectToAction("AdminListAdvert");
                                 }
                                 catch
                                 {
                                     var pic = AdvertRepository.getAdvertById(id);
                                     return View(pic);
                                 }
                             }
       
                                public ActionResult EditImage(int id)
                                {
                                    try
                                    {
                                        Advertcurent = id;
                                        var pic = AdvertRepository.getAdvertById(id);
                                        return View(pic);
                                    }
                                    catch (Exception)
                                    {
                                        return RedirectToAction("AdminListAdvert");
                                    }

                                }

                                  [HttpPost]
                                  public ActionResult EditImages()
                                  {
                                      try
                                      {
                                          var wok = AdvertRepository.getAdvertById(Advertcurent);
                                          #region upload images
                                          Uploader uploader = new Uploader();
                                          uploader.UploadPath = Server.MapPath("~\\Content\\AdvertImages");
                                          uploader.IsLowerName = false;
                                          uploader.IsEncryptName = false;
                                          uploader.IsOverwrite = false;
                                          uploader.Prefix = "";
                                          uploader.Suffix = "";

                                          uploader.AllowedExtensions.Add(".jpg");
                                          uploader.AllowedExtensions.Add(".jpeg");
                                          uploader.AllowedExtensions.Add(".gif");
                                          uploader.AllowedExtensions.Add(".png");

                                          bool success = uploader.DoUpload("MyFile");
                                          #endregion
                                          string abc = uploader.PostedFile.FileName;
                                          db.Adverts_update(wok.Id,wok.AdvertName,wok.Description,abc,wok.Link,wok.Priority,wok.Show);
                                          return RedirectToAction("Edit", new { id = Advertcurent });
                                      }
                                      catch (Exception)
                                      {
                                          return RedirectToAction("EditImage", new { id= Advertcurent});
                                      }
           
                                  }
        
    }
}
